﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ajayumi.Platform.Search
{
    /// <summary>
    /// 查询项
    /// Author   : 黄乙冬
    /// Date     : 2011-09-19
    /// </summary>
    [Serializable]
    public class SearchItem
    {
        /// <summary>
        /// 数据库的表名或是表的别名
        /// </summary>
        public string TableName
        {
            get;
            set;
        }

        /// <summary>
        /// 字段名称
        /// </summary>
        public string FieldName
        {
            get;
            set;
        }

        /// <summary>
        /// 显示名称
        /// </summary>
        public string DisplayName
        {
            get;
            set;
        }

        public SearchItemValue Value
        {
            get;
            set;
        }


        /// <summary>
        /// 默认值
        /// </summary>
        public string DefaultValue
        {
            get;
            set;
        }

        /// <summary>
        /// 当Value.Value1为该值时，忽略掉，不拼接语句
        /// </summary>
        public string IgnoredValue
        {
            get;
            set;
        }

        /// <summary>
        /// 条件
        /// </summary>
        public string Condition
        {
            get;
            set;
        }

        /// <summary>
        /// 可选择项
        /// </summary>
        public SearchOption[] SearchOptions
        {
            get;
            set;
        }

        /// <summary>
        /// 可选择项
        /// </summary>
        public SearchOption[] SearchOptions2
        {
            get;
            set;
        }

        /// <summary>
        /// 是否是或条件,不是OR就是AND
        /// </summary>
        public bool IsOr
        {
            get;
            set;
        }

        /// <summary>
        /// 值的类型
        /// </summary>
        public ValueType ValueType
        {
            get;
            set;
        }

        /// <summary>
        /// 创建控件的类型
        /// </summary>
        public ValueControlType ControlType
        {
            get
            {
                if (Condition == Conditions.Between)
                {
                    //下拉框
                    if (SearchOptions != null && SearchOptions.Length > 0)
                    {
                        return ValueControlType.RangeDropdownList;
                    }
                    else if (ValueType == ValueType.Date)
                    {
                        return ValueControlType.RangeDateTimePicker;
                    }
                    else
                    {
                        return ValueControlType.RangeTextBox;
                    }
                }
                else
                {
                    //下拉框
                    if (SearchOptions != null && SearchOptions.Length > 0)
                    {
                        return ValueControlType.DropdownList;
                    }
                    else if (ValueType == ValueType.Date)
                    {
                        return ValueControlType.DateTimePicker;
                    }
                    else
                    {
                        return ValueControlType.TextBox;
                    }
                }
            }
        }

        public virtual string ToSql()
        {
            //没值的就不要有查询条件
            if (Value == null || string.IsNullOrEmpty(Value.Value1))
            {
                return string.Empty;
            }

            if (Value.Value1 == IgnoredValue)
            {
                return string.Empty;
            }

            StringBuilder sbSql = new StringBuilder();

            if (!string.IsNullOrEmpty(TableName))
            {
                sbSql.Append(TableName);
                sbSql.Append(".");
            }

            sbSql.Append(FieldName);

            if (Condition == Conditions.Between)
            {
                if (!string.IsNullOrEmpty(Value.Value1) && !string.IsNullOrEmpty(Value.Value2))
                {
                    sbSql.Append(" BETWEEN ");
                    sbSql.Append(GetValueString(Value.Value1));
                    sbSql.Append(" AND ");
                    sbSql.Append(GetValueString(Value.Value2));
                }
                else if (!string.IsNullOrEmpty(Value.Value1))
                {
                    sbSql.Append(" >= ");
                    sbSql.Append(GetValueString(Value.Value1));
                }
                else
                {
                    sbSql.Append(" <= ");
                    sbSql.Append(GetValueString(Value.Value2));
                }
            }
            else if (Condition == Conditions.Like || Condition == Conditions.NotLike)
            {
                sbSql.Append(" ");
                sbSql.Append(Condition);
                sbSql.Append(" ");
                sbSql.Append(GetLikeValueString(Value.Value1));
                sbSql.Append(" ");
            }
            else
            {
                sbSql.Append(" ");
                sbSql.Append(Condition);
                sbSql.Append(" ");
                sbSql.Append(GetValueString(Value.Value1));
                sbSql.Append(" ");
            }

            return sbSql.ToString();

        }

        private string GetValueString(string value)
        {
            if (ValueType == ValueType.Number)
            {
                return value;
            }
            else
            {
                return "N'" + value + "'";
            }
        }

        private string GetLikeValueString(string value)
        {
            string parm = SafeParameter(value);
            return "N'%" + parm + "%'";
        }
        /// <summary>
        /// 参数的过滤
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string SafeParameter(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return value;
            }

            return value.Replace("'", "''");
        }
    }
}
